package com.beiding.workordersystem.orders.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.common.utils.TreeUtils;
import com.beiding.workordersystem.orders.product.dao.ProductDao;
import com.beiding.workordersystem.orders.product.dao.ProductTypeDao;
import com.beiding.workordersystem.orders.product.entity.Product;
import com.beiding.workordersystem.orders.product.entity.ProductType;
import com.beiding.workordersystem.orders.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl extends BaseService implements ProductService {

    @Autowired
    private ProductTypeDao typeDao;

    @Autowired
    private ProductDao productDao;

    //返回所有子节点
    @Override
    public ProductType treeType() {
        ProductType one = typeDao.selectOne(QueryUtils.getQuery(ProductType.class).eq(ProductType::getHierarchy, 0));
        if (one == null) {
            synchronized (this) {
                one = typeDao.selectOne(QueryUtils.getQuery(ProductType.class).eq(ProductType::getHierarchy, 0));
                if (one == null) {
                    one = new ProductType();
                    one.setId("0");
                    one.setName("全部");
                    one.setHierarchy(0);
                    fillId(one);
                    typeDao.insert(one);
                }
            }
        }
        TreeUtils.fillChildren(one, "children", o -> typeDao.selectList(QueryUtils.getQuery(o).eq(ProductType::getPid, o.getId())));
        return one;
    }


    @Override
    public void saveType(ProductType productType) {
        if (productType.getPid().equals("0")) {
            productType.setHierarchy(1);
        } else {
            ProductType p = typeDao.selectById(productType.getPid());
            E.notNull(p, "给定的父节点不存在");
            productType.setHierarchy(p.getHierarchy());
        }

        E.isFalse(typeDao.exist(QueryUtils.getQuery(productType).eq(ProductType::getPid, productType.getPid()).eq(ProductType::getName, productType.getName())), "同一个父类下不能具有两个相同的子类");

        if (fillId(productType)) {
            typeDao.insert(productType);
        } else {
            typeDao.updateById(productType);
        }
    }

    @Override
    @Transactional
    public void deleteType(String[] ids) {
        for (String id : ids) {
            if (typeDao.exist(QueryUtils.getQuery(ProductType.class).eq(ProductType::getId, id))) {
                E.isFalse(productDao.exist(QueryUtils.getQuery(Product.class).eq(Product::getTypeId, id)), "类型下关联多个产品,无法删除");
                E.isFalse(typeDao.exist(QueryUtils.getQuery(ProductType.class).eq(ProductType::getPid, id)), "类型下存在子类,无法删除");
                typeDao.deleteById(id);
            }
        }
    }

    @Override
    public String save(Product copy) {
        String typeId = copy.getTypeId();
        E.isFalse(typeId.equals("0"), "不能使用根节点作为类型");
        E.isTrue(typeDao.exist(QueryUtils.getQuery(ProductType.class).eq(ProductType::getId, typeId)), "指定的产品类型不存在");
        if (fillId(copy)) {
            productDao.insert(copy);
        } else {
            productDao.updateById(copy);
        }
        return copy.getId();
    }

    @Override
    public IPage page(MPage page, String typeId) {
        if (typeId.equals("0")) {
            return productDao.selectPage(page.toIPage(), new QueryWrapper<>());
        } else {
            return productDao.selectPage(page.toIPage(), QueryUtils.getQuery(Product.class).eq(Product::getTypeId, typeId));
        }
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        for (String id : ids) {
            //强制删除即可
            productDao.deleteById(id);
        }
    }

    @Override
    public Product findById(String id) {
        Product product = productDao.selectById(id);
        if (notEmpty(product)) {
            fillTypeName(product,product.getTypeId());
        }
        return product;
    }

    private void fillTypeName(Product product,String typeId) {
        ProductType productType = typeDao.selectById(typeId);
        if (productType == null) {
            return;
        }

        if (productType.getHierarchy() == 1) {
            product.setTypeName(productType.getName());
            return;
        }

        fillTypeName(product, productType.getPid());

    }

    @Override
    public List<Product> list() {
        List<Product> products = productDao.selectList(new QueryWrapper<>());
        for (Product product : products) {
            fillTypeName(product, product.getTypeId());
        }
        return products;
    }
}
